import {Component, OnInit, ElementRef, OnDestroy} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';
import {Router} from '@angular/router';
import {NormalService} from '../../../apiServices/normal.service';
import { S3UploaderService } from 'ngx-s3-upload'; // 引入s3-uploader

declare let $: any;

@Component({
    selector: 'real-authentication',
    templateUrl: './real-authentication.template.html'
})
export class RealAuthenticationComponent implements OnInit, OnDestroy{
    public language: string = 'en';
    public user: any;
    private uploadToken: string;
    public realname: any = {};
    public checkVerifyOnce: boolean;
    public userSubscription: Subscription;
    public countries: Array<any> = [
        {en:"Angola",cn:"安哥拉"},
        {en:"Afghanistan",cn:"阿富汗"},
        {en:"Albania", cn:"阿尔巴尼亚"},
        {en:"Algeria", cn:"阿尔及利亚"},
        {en:"Andorra",cn:"安道尔共和国"},
        {en:"Anguilla",cn:"安圭拉岛"},
        {en:"Antigua and Barbuda",cn:"安提瓜和巴布达"},
        {en:"Argentina",cn:"阿根廷"},
        {en:"Armenia",cn:"亚美尼亚"},
        {en:"Ascension",cn:"阿森松"},
        {en:"Australia",cn:"澳大利亚"},
        {en:"Austria",cn:"奥地利"},
        {en:"Azerbaijan",cn:"阿塞拜疆"},
        {en:"Bahamas",cn:"巴哈马"},
        {en:"Bahrain",cn:"巴林"},
        {en:"Bangladesh",cn:"孟加拉国"},
        {en:"Barbados",cn:"巴巴多斯"},
        {en:"Belarus",cn:"白俄罗斯"},
        {en:"Belgium",cn:"比利时"},
        {en:"Belize",cn:"伯利兹"},
        {en:"Benin",cn:"贝宁"},
        {en:"Bermuda Is",cn:"百慕大群岛"},
        {en:"Bolivia",cn:"玻利维亚"},
        {en:"Botswana",cn:"博茨瓦纳"},
        {en:"Brazil",cn:"巴西"},
        {en:"Brunei",cn:"文莱"},
        {en:"Bulgaria",cn:"保加利亚"},
        {en:"Burkina Faso",cn:"布基纳法索"},
        {en:"Burma",cn:"缅甸"},
        {en:"Burundi",cn:"布隆迪"},
        {en:"Cameroon",cn:"喀麦隆"},
        {en:"Canada",cn:"加拿大"},
        {en:"Cayman Is",cn:"开曼群岛"},
        {en:"Central African Republic",cn:"中非共和国"},
        {en:"Chad",cn:"乍得"},
        {en:"Chile",cn:"智利"},
        {en:"China",cn:"中国"},
        {en:"Colombia",cn:"哥伦比亚"},
        {en:"Congo",cn:"刚果"},
        {en:"Cook Is",cn:"库克群岛"},
        {en:"Costa Rica",cn:"哥斯达黎加"},
        {en:"Cuba",cn:"古巴"},
        {en:"Cyprus",cn:"塞浦路斯"},
        {en:"Czech Republic",cn:"捷克"},
        {en:"Denmark",cn:"丹麦"},
        {en:"Djibouti",cn:"吉布提"},
        {en:"Dominica Rep",cn:"多米尼加共和国"},
        {en:"Ecuador",cn:"厄瓜多尔"},
        {en:"Egypt",cn:"埃及"},
        {en:"EI Salvador",cn:"萨尔瓦多"},
        {en:"Estonia",cn:"爱沙尼亚"},
        {en:"Ethiopia",cn:"埃塞俄比亚"},
        {en:"Fiji",cn:"斐济"},
        {en:"Finland",cn:"芬兰"},
        {en:"France",cn:"法国"},
        {en:"French Guiana",cn:"法属圭亚那"},
        {en:"French Polynesia",cn:"法属玻利尼西亚"},
        {en:"Gabon",cn:"加蓬"},
        {en:"Gambia",cn:"冈比亚"},
        {en:"Georgia",cn:"格鲁吉亚"},
        {en:"German",cn:"-德国"},
        {en:"Ghana",cn:"加纳"},
        {en:"Gibraltar",cn:"直布罗陀"},
        {en:"Greece",cn:"希腊"},
        {en:"Grenada",cn:"格林纳达"},
        {en:"Guam",cn:"关岛"},
        {en:"Guatemala",cn:"危地马拉"},
        {en:"Guinea",cn:"几内亚"},
        {en:"Guyana",cn:"圭亚那"},
        {en:"Haiti",cn:"海地"},
        {en:"Honduras",cn:"洪都拉斯"},
        {en:"Hongkong",cn:"香港"},
        {en:"Hungary",cn:"匈牙利"},
        {en:"Iceland",cn:"冰岛"},
        {en:"India",cn:"印度"},
        {en:"Indonesia",cn:"印度尼西亚"},
        {en:"Iran",cn:"伊朗"},
        {en:"Iraq",cn:"伊拉克"},
        {en:"Ireland",cn:"爱尔兰"},
        {en:"Israel",cn:"以色列"},
        {en:"Italy",cn:"意大利"},
        {en:"Ivory Coast",cn:"科特迪瓦"},
        {en:"Jamaica",cn:"牙买加"},
        {en:"Japan",cn:"日本"},
        {en:"Jordan",cn:"约旦"},
        {en:"Kampuchea (Cambodia )",cn:"柬埔寨"},
        {en:"Kazakstan",cn:"哈萨克斯坦"},
        {en:"Kenya",cn:"肯尼亚"},
        {en:"Korea",cn:"韩国"},
        {en:"Kuwait",cn:"科威特"},
        {en:"Kyrgyzstan",cn:"吉尔吉斯坦"},
        {en:"Laos",cn:"老挝"},
        {en:"Latvia",cn:"拉脱维亚"},
        {en:"Lebanon",cn:"黎巴嫩"},
        {en:"Lesotho",cn:"莱索托"},
        {en:"Liberia",cn:"利比里亚"},
        {en:"Libya",cn:"利比亚"},
        {en:"Liechtenstein",cn:"列支敦士登"},
        {en:"Lithuania",cn:"立陶宛"},
        {en:"Luxembourg",cn:"卢森堡"},
        {en:"Macao",cn:"澳门"},
        {en:"Madagascar",cn:"马达加斯加"},
        {en:"Malawi",cn:"马拉维"},
        {en:"Malaysia",cn:"马来西亚"},
        {en:"Maldives",cn:"马尔代夫"},
        {en:"Mali",cn:"马里"},
        {en:"Malta",cn:"马耳他"},
        {en:"Mariana Is",cn:"马里亚那群岛"},
        {en:"Martinique",cn:"马提尼克"},
        {en:"Mauritius",cn:"毛里求斯"},
        {en:"Mexico",cn:"墨西哥"},
        {en:"Moldova",cn:"摩尔多瓦"},
        {en:"Monaco",cn:"摩纳哥"},
        {en:"Mongolia",cn:"蒙古"},
        {en:"Montserrat Is",cn:"蒙特塞拉特岛"},
        {en:"Morocco",cn:"摩洛哥"},
        {en:"Mozambique",cn:"莫桑比克"},
        {en:"Namibia",cn:"纳米比亚"},
        {en:"Nauru",cn:"瑙鲁"},
        {en:"Nepal",cn:"尼泊尔"},
        {en:"Netheriands Antilles",cn:"荷属安的列斯"},
        {en:"Netherlands",cn:"荷兰"},
        {en:"New Zealand",cn:"新西兰"},
        {en:"Nicaragua",cn:"尼加拉瓜"},
        {en:"Niger",cn:"尼日尔"},
        {en:"Nigeria",cn:"尼日利亚"},
        {en:"North Korea",cn:"朝鲜"},
        {en:"Norway",cn:"挪威"},
        {en:"Oman",cn:"阿曼"},
        {en:"Pakistan",cn:"巴基斯坦"},
        {en:"Panama",cn:"巴拿马"},
        {en:"Papua New Cuinea",cn:"巴布亚新几内亚"},
        {en:"Paraguay",cn:"巴拉圭"},
        {en:"Peru",cn:"秘鲁"},
        {en:"Philippines",cn:"菲律宾"},
        {en:"Poland",cn:"波兰"},
        {en:"Portugal",cn:"葡萄牙"},
        {en:"Puerto Rico",cn:"波多黎各"},
        {en:"Qatar",cn:"卡塔尔"},
        {en:"Reunion",cn:"留尼旺"},
        {en:"Romania",cn:"罗马尼亚"},
        {en:"Russia",cn:"俄罗斯"},
        {en:"Saint Lueia",cn:"圣卢西亚"},
        {en:"Saint Vincent",cn:"圣文森特岛"},
        {en:"Samoa Eastern",cn:"东萨摩亚(美)"},
        {en:"Samoa Western",cn:"西萨摩亚"},
        {en:"San Marino",cn:"圣马力诺"},
        {en:"Sao Tome and Principe",cn:"圣多美和普林西比"},
        {en:"Saudi Arabia",cn:"沙特阿拉伯"},
        {en:"Senegal",cn:"塞内加尔"},
        {en:"Seychelles",cn:"塞舌尔"},
        {en:"Sierra Leone",cn:"塞拉利昂"},
        {en:"Singapore",cn:"新加坡"},
        {en:"Slovakia",cn:"斯洛伐克"},
        {en:"Slovenia",cn:"斯洛文尼亚"},
        {en:"Solomon Is",cn:"所罗门群岛"},
        {en:"Somali",cn:"索马里"},
        {en:"South Africa",cn:"南非"},
        {en:"Spain",cn:"西班牙"},
        {en:"SriLanka",cn:"斯里兰卡"},
        {en:"St.Lucia",cn:"圣卢西亚"},
        {en:"St.Vincent",cn:"圣文森特"},
        {en:"Sudan",cn:"苏丹"},
        {en:"Suriname",cn:"苏里南"},
        {en:"Swaziland",cn:"斯威士兰"},
        {en:"Sweden",cn:"瑞典"},
        {en:"Switzerland",cn:"瑞士"},
        {en:"Syria",cn:"叙利亚"},
        {en:"Taiwan",cn:"台湾省"},
        {en:"Tajikstan",cn:"塔吉克斯坦"},
        {en:"Tanzania",cn:"坦桑尼亚"},
        {en:"Thailand",cn:"泰国"},
        {en:"Togo",cn:"多哥"},
        {en:"Tonga",cn:"汤加"},
        {en:"Trinidad and Tobago",cn:"特立尼达和多巴哥"},
        {en:"Tunisia",cn:"突尼斯"},
        {en:"Turkey",cn:"土耳其"},
        {en:"Turkmenistan",cn:"土库曼斯坦"},
        {en:"Uganda",cn:"乌干达"},
        {en:"Ukraine",cn:"乌克兰"},
        {en:"United Arab Emirates",cn:"阿拉伯联合酋长国"},
        {en:"United Kiongdom",cn:"英国"},
        {en:"United States of America",cn:"美国"},
        {en:"Uruguay",cn:"乌拉圭"},
        {en:"Uzbekistan",cn:"乌兹别克斯坦"},
        {en:"Venezuela",cn:"委内瑞拉"},
        {en:"Vietnam",cn:"越南"},
        {en:"Yemen",cn:"也门"},
        {en:"Yugoslavia",cn:"南斯拉夫"},
        {en:"Zimbabwe",cn:"津巴布韦"},
        {en:"Zaire",cn:"扎伊尔"},
        {en:"Zambia",cn:"赞比亚"}
    ];
    
    isVerified: any = {en: 'you have already verified !', zh: '您已通过实名认证！'};
    isUploaded: any = {en: 'you have already uploaded, please wait for the investigate !', zh: '您已提交实名认证申请，请耐心等待通过审核！'};
    hasCredentral: any = {en: 'You have not Credentials uploaded !', zh: '您还未上传证件照片！'};
    isInforcomplete: any = {en: 'All infos must be fill in !', zh: '信息填写必须完整！'};

    constructor(
        private el: ElementRef,
        private normalService: NormalService,
        private router: Router,
        private s3UploaderService: S3UploaderService,
    ) {
        this.language = normalService.lang;
        if(normalService.isLogin()){
            normalService.getuser();
        }
        this.userSubscription = normalService.user$.subscribe(user=>{
            this.user = user;
            if (user.verified == true) {
                if(!this.checkVerifyOnce){
                    this.checkVerifyOnce = !this.checkVerifyOnce;
                    if(this.normalService.lang == 'en'){
                        alert(this.isVerified.en);
                    }
                    if(this.normalService.lang == 'zh'){
                        alert(this.isVerified.zh);
                    }

                }
                this.router.navigate(['/account']);
            }
            if (user.attestation && user.attestation.state === 0) {
                if(!this.checkVerifyOnce){
                    this.checkVerifyOnce = !this.checkVerifyOnce;
                    if(this.normalService.lang == 'en'){
                        alert(this.isUploaded.en);
                    }
                    if(this.normalService.lang == 'zh'){
                        alert(this.isUploaded.zh);
                    }
                }
                this.router.navigate(['/account']);
            }
        });
    }

    /**
     * 上传文件的方法
     * @param file 
     * @param filename 
     */
    private uploadfile(file, filename, callback): void {
        filename = "realname/" + filename;
        this.s3UploaderService.upload(file, 'public-read', 'wakandacar', filename)
            .subscribe(
                (data) => {
                    console.log('data', data);
                    callback(data);
                },
                (error) => {
                    console.error('error', error);
                });
    }

    /**
     * 文件变更的上传事件
     */
    fileOnChange($event, input, name): void {
        var idx = input.files[0].name.lastIndexOf(".");
        var suffix = input.files[0].name.substring(idx);
        var stamp = new Date().valueOf();
        this.uploadfile(input.files[0], (stamp+suffix), (data)=>{
            this.realname[name] = data.Location;
        });
    }

    /**
     * 更换身份验证的图片
     * @param imageInput input对象·
     */
    changeImage(imageInput): void {
        $(imageInput).click();
    }
    
    submitReal(): void {
        if (!this.realname.credentials){
            if(this.normalService.lang == 'en')
                alert(this.isUploaded.en);
            if(this.normalService.lang == 'zh')
                alert(this.isUploaded.zh);
        } 
        if (!this.realname.name || !this.realname.idNumber || !this.realname.country){
            if(this.normalService.lang == 'en')
                alert(this.isInforcomplete.en);
            if(this.normalService.lang == 'zh')
                alert(this.isInforcomplete.zh);
        }   
        this.realname.state = 0;
        this.normalService.updateuser(this.user._id, {
            $set: {
                attestation: this.realname
            }
        }, {}).then(user=>{
            this.router.navigate(['/account']);
        }).catch((error) => {
            alert(this.normalService.getErrors(error));
            this.normalService.loginOut();
        });
    }
    
    ngOnInit(): void {
        this.normalService.getuser();
    }

    ngOnDestroy(): void {
        this.userSubscription.unsubscribe();
    }
}